<template>
  <Modal v-model="visible" width="900" title="回款核销">
    <div class="top">
    	<RadioGroup v-model="sortType" @on-change="onChange">
        <Radio label="contract_date">按签约日期排序</Radio>
        <Radio label="predict_account_Date">按全款日期排序</Radio>
      </RadioGroup>
      <Button type="primary" @click="onBatch">批量选择</Button>
    </div>
    
    <dt-grid ref="table" :columns="columns" :list="list" :select="select" :height="500" />
    
    <div class="bottom">
      <p>已选择房源{{ selection.list.length }}套</p>
      <p>
        本次可核销总金额{{ $dt.tool.formatterMoney(writeoffTotal) }}元，
        已选核销金额{{ $dt.tool.formatterMoney(selection.total) }}元
      </p>
    </div>
    
    <div slot="footer" class="footer">
      <Button @click="hide">取消</Button>
      <Button type="primary" @click="onConfirm">确认</Button>
    </div>
    
    <import-modal ref="import" @success="onSuccess" />
  </Modal>
</template>

<script>
  import importModal from './import-modal.vue'
  import columnOpt from '../opt/writeoffColumn.js'
  import calcFun from '../opt/calc.js'
  import informationMgr from '@/biz/commission/information.js'

  export default {
    props: {
      refresh: Function
    },
    components: {
      importModal
    },
    data() {
      return {
        visible: false,
        sortType: 'contract_date',
				columns: columnOpt.batchWriteoffHeaders(this),
				list: [],
        selection: {
          list: [],
          total: 0
        },
        writeoffTotal: 0,
        commissionId: '',
        select: {
          one: (selection, row) => {
            if (selection.some(i => i.commissionDetailId == row.commissionDetailId)) {
              let actualAgentCost = row.actualAgentCost ? Number(row.actualAgentCost) : 0
              let total = this.$tool.precision(Number(this.selection.total) + actualAgentCost)
              if (total > this.writeoffTotal) {
                this.$Message.warning('无法选择，本次核销金额已超过本次可核销总金额！')
                this.$refs.table.onClearSelection()
                this.$refs.table.selectRows(this.selection.list)
              } else {
                this.selection = {
                  list: selection,
                  total
                }
              }
            } else {
              let actualAgentCost = row.actualAgentCost ? Number(row.actualAgentCost) : 0
              this.selection = {
                list: selection,
                total: Number(this.selection.total) - actualAgentCost
              }
            }
          },
          all: (selection) => {
            if (selection.length > 0) {
              let total = selection.reduce((num, { actualAgentCost }) => {
                return actualAgentCost ? num + Number(actualAgentCost) : num
              }, 0)
              if (this.$tool.precision(total) > this.writeoffTotal) {
                this.$Message.warning('无法选择，本次核销金额已超过本次可核销总金额！')
                this.$refs.table.onClearSelection()
                this.$refs.table.selectRows(this.selection.list)
              } else {
                this.selection = {
                  list: selection,
                  total: this.$tool.precision(total)
                }
              }
            } else {
              this.selection = {
                list: [],
                total: 0
              }
            }
          }
        },
      }
    },
    methods: {
      onConfirm() {
        if (this.selection.list.length <= 0) {
          this.$Message.warning('请选择房源')
          return
        }
        if (this.selection.total > this.writeoffTotal) {
          this.$Message.warning('本次核销金额已超过本次可核销总金额')
          return
        }
        this.$emit('selection', this.selection)
        this.hide()
      },
      onBatch() {
        this.$refs.import.show({
          commissionId: this.commissionId
        })
      },
      onSuccess(data) {
        let selection = [], total = 0
        for (let item of data) {
          let d = this.list.find(i => i.roomId == item.roomId)
          if (d) {
            selection.push(d)
            total = d.actualAgentCost ? total + Number(d.actualAgentCost) : total
          }
        }
        this.selection = {
          list: selection,
          total: this.$tool.precision(total)
        }
        this.$refs.table.onClearSelection()
        this.$refs.table.selectRows(this.selection.list)
      },
      // 初始化选中的房源
      initSelection() {
        let data = informationMgr.initSelection(this.list, this.writeoffTotal)
        this.selection = {
          list: data.selection,
          total: data.total
        }
        this.$refs.table.selectRows(this.selection.list)
      },
      // 切换排序类型
      onChange() {
        this.getList()
      },
      // 查询列表
      getList() {
        informationMgr.writeOffRoomList({
          commissionId: this.commissionId,
          orderBy: this.sortType
        }).then(res => {
          this.list = res
          this.initSelection()
        })
      },
      show(info) {
        this.visible = true
        this.commissionId = info.commissionId
        this.list = [...info.list]
        this.selection = { ...info.selection }
        this.writeoffTotal = info.writeoffTotal
        this.$refs.table.onClearSelection()
        this.$refs.table.selectRows(this.selection.list)
      },
      hide() {
        this.visible = false
      },
    },
  }
</script>

<style lang='scss' scoped>
  .top {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
  }
  
  .bottom {
    margin-top: 10px;
  }
  
  .footer {
    text-align: center;
  }
</style>